# -*- coding: utf-8 -*-
from xTkinter import *
import tkFileDialog
import tkMessageBox as msgBox
import sys
from Validation_Contraintes import *
Validateur = ValidationContrainte()
import xmlrpclib
class VueSaisieChamps(Frame):
    def __init__(self,master,parent,**cnf):
        self.ListeChamps=[]
        self.ListEntries=[]
        self.ListLabels=[]
        self.ListeContraintes = []
        self.parent = parent
        self.master=master
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=800,height=600)        
        self.Bouton=xButton(self,text="Ajouter",width=12, command=self.ajouterData)
        self.Bouton.setLocation(0,0)
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(135,0)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirChamps)
        self.CBTables.setText('<Choisir>')
        
        
    def ajouterData(self):
        Valeurs = {}
        Valide = 1
        for i in range(len(self.ListeChamps)):
            Valeurs[self.ListeChamps[i][0]] = self.ListEntries[i].get()
            for contraintes in self.ListeContraintes:
                if contraintes["ATTRIBUT_GAUCHE"] == self.ListeChamps[i][0]:
                    fonction = Validateur.Operande[contraintes['COMPARAISON']]
                    if (fonction(int(self.ListEntries[i].get()),int(contraintes['OPERANDE_DROITE'])) == 0):
                        msgBox.showerror('Saisie de données',contraintes['MESSAGE_ERREUR'] )
                        Valide = 0
        if (Valide == 1):
            self.parent.serveur.InsererEnregistrement(self.CBTables.getText(), Valeurs)
            msgBox.showerror('Saisie de données','Les données ont été correctemnet insérées' )
        pass
    
    def obtenirTables(self):
        column=0 
        Reponse = self.parent.serveur.ObtenirListeTables()
        print Reponse
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i])
                self.CBTables.setText(self.ListeChamps[i])
        
    def obtenirChamps(self):
        self.ListeChamps=[]
        self.ListeContraintes = []
        for i in range(len(self.ListEntries)):
            self.ListEntries[i].destroy()
            self.ListLabels[i].destroy()

        self.ListEntries=[]
        self.ListLabels=[]
        table=self.CBTables.getText();
        Reponse = self.parent.serveur.ObtenirListeChamps(table)
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.ListLabels.append(xLabel(self,text=self.ListeChamps[i][0], width=len(self.ListeChamps[i][0])+2, anchor=SW ))
                self.ListLabels[len(self.ListLabels)-1].setLocation(0,(i+1)*25+5)
            for i in range(len(self.ListeChamps)):
                self.ListEntries.append(xEntry(self,width=self.ListeChamps[i][1]))
                self.ListEntries[len(self.ListEntries)-1].setLocation(120,(i+1)*25+5)
            Reponse = self.parent.serveur.ObtenirContraintes(self.CBTables.getText())
            if(Reponse[0]==1):
                  self.ListeContraintes = Reponse[1]
            
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
           
        
    def close(self):
        self.destroy()
        pass

    
class Controleur:
    def __init__(self):
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        root=Tk()
        root.geometry('800x400')
        vi=VueSaisieChamps(root,self)
        root.mainloop()       

Controleur = Controleur()
     
        
    

            



    
    
    
